import { defineStore } from 'pinia';
import { login } from '@/api/Public';
import { getUserDepartment } from '@/api/PersonnelBindingItems/index';
import Cookies from 'js-cookie';
export const useUserStore = defineStore('user', {
	state: () => ({
		backadmin: {}, //用户信息
		token: '', //token
		admin_id: '', //后台登录人员id
		departmentId: '' //部门id
	}),
	actions: {
		//登录
		async login() {
			let res = await login();
			this.backadmin = res.data.userinfo;
			this.token = res.data.userinfo.token;
		},
		//根据后台id获取部门id
		async getDepartmentId() {
			//获取cookie的hehe_back_user_id
			this.admin_id = Cookies.get('hehe_back_user_id');
			console.log('admin_id', this.admin_id);
			//获取管理员所属部门
			let res = await getUserDepartment({ admin_id: this.admin_id });
			this.departmentId = res.data;
			console.log('departmentId', this.departmentId);
		}
	},
	persist: {
		enabled: true,
		strategies: [
			{
				key: 'user',
				storage: localStorage
			}
		]
	}
});
